package edu.uidaho.pong;

import java.io.IOException;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.os.Handler;
import android.os.Message;
import android.widget.TextView;
import android.widget.Toast;
import android.app.Activity;

import android.view.SurfaceHolder;

public class GameThread extends Thread {


    /** Handle to the surface manager object we interact with */
    private SurfaceHolder _surfaceHolder;
    private Paint _paint;
    private GameState _state;
    public PongActivity _pong;
    public TextView mResponseField;
    
    
    

    public GameThread(SurfaceHolder surfaceHolder, Context context,
            Handler handler) {
        _surfaceHolder = surfaceHolder;
        _paint = new Paint();
        _state = new GameState();
        _pong = new PongActivity();
        mResponseField = _pong.mResponseField;
        
        
        

    }

//  @Override
    public void run() {
        while (true) {
            Canvas canvas = _surfaceHolder.lockCanvas();
            _state.update(_pong);
            _state.draw(canvas, _paint);
            _surfaceHolder.unlockCanvasAndPost(canvas);

//    		_pong.runOnUiThread(new Runnable() {
//
//                @Override
//                public void run() {
//                    _pong.mResponseField.setText("Reading: ");
//                }
//            });
    		
    		
            int ret = 0;
            byte[] buffer = new byte[16384];
            int i;

            //int tempdebug = 0;
            while (true) { // read data
            	//while (tempdebug++ < 10){
            	try {
            		ret = _pong.mInputStream.read(buffer);
            	} catch (IOException e) {
            		break;
            	}


            	i = 0;
            	while (i < ret) {
            		int len = ret - i;
            		if (len >= 1) {
            			Message m = Message.obtain(_pong.mHandler);
            			int value = (int)buffer[i];
            			// 'f' is the flag, use for your own logic
            			// value is the value from the arduino
            			m.obj = new ValueMsg('f', value);
            			_pong.mHandler.sendMessage(m);
            		}
            		i += 1; // number of bytes sent from arduino
            	}
            }
            //_pong.mResponseField.setText("Reading: "); //This line causes a crash Something about not being able to change text views from a thread ~~ related issue: runningUiOnThread()
            //_pong.printText("This better @#$%ing work"); // Causes a crash...
            
            
//            _pong.runOnUiThread(new Runnable() {
//
//                @Override
//                public void run() {
//                    _pong.mResponseField.setText("Reading: ");
//                }
//            });
            
        }
    }

    public GameState getGameState() {
        return _state;
    }
}